import 'package:data_plugin/bmob/bmob_query.dart';
import 'package:data_plugin/bmob/realtime/change.dart';
import 'package:data_plugin/bmob/realtime/client.dart';
import 'package:data_plugin/bmob/realtime/real_time_data_manager.dart';
import 'package:data_plugin/bmob/response/bmob_error.dart';
import 'package:data_plugin/bmob/table/bmob_object.dart';
/// CreateTime 2020/6/3 13:07
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/utils/toast_utils.dart';
import 'package:flutter_elf/widgets/common_widgets.dart';

class SecondBmobPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SecondBmobPageState();
  }
}

class SecondBmobPageState extends State{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBarCreator.create(context, '后端云'),
      body: Column(
        children: <Widget>[
          buildButton('保存user', ()async{
            User user = User();
            user.sName = '张美';
            user.iAge = 30;
            user.bIsMale = false;
            user.save().then((res){
              print(res);
              showToast('保存成功');
            });
          }),

          buildButton('关联', (){
            User user = User();
            user.sName = '呵呵哒';
//            user.objectId = '2046eb28f1';///一定要关联id
            user.iAge = 30;
            user.bIsMale = true;
            Blog blog = Blog();
            blog.title = '呵呵哒的文章';
            blog.content = '呵呵哒的内容';
            ///下边一定是关联的blog去save
            blog.save().then((res){
              print(res);
              showToast('保存成功');
            });
          }),

          buildButton('查询整个userinfo',(){
            _queryUser();
          }),

          buildButton('查询关联',(){
            _queryPointer();
          }),
        ],
      ),
    );
  }

  ///数据监听
  _listen() {
    RealTimeDataManager.getInstance().listen(onConnected: (Client client) {
      showToast( "监听数据连接成功，开始订阅消息！");
      client.subTableUpdate("Blog");
    }, onDisconnected: () {
      showToast( "监听数据断开连接");
    }, onDataChanged: (Change data) {
      ///注意：此处返回的data.data类型与Blog类型不一致，需要使用map来获取具体属性值而不是使用Blog
      Map map = data.data;
      showToast( "监听到数据变化：" + map.toString());
    }, onError: (error) {
      showToast( error.toString());
    });
  }

  _queryUser(){
    BmobQuery<User> query = BmobQuery();
    query.queryObjects().then(((res){
      print(res);
    }));
  }

  ///查询关联数据
  _queryPointer() {
    BmobQuery<Blog> query = BmobQuery();
//    query.setInclude("sName");
    query.addWhereEqualTo('content', '内容');
    query.queryObjects().then((data) {
      showToast("查询成功${data.length}");
      print(data);
    }).catchError((e) {
      showToast(BmobError.convert(e).error);
    });
  }

}

class User extends BmobObject{
  String sName;
  int iAge;
  bool bIsMale;
  Blog blog;
  @override
  Map getParams() {
    Map<String,dynamic> map = {
      "sName":this.sName,
      "iAge":this.iAge,
      "bIsMale":this.bIsMale,
      "objectId":objectId,
      "blog":blog,
    };
    return map;
  }
}
class Blog extends BmobObject{

  String title;
  String content;

  @override
  Map getParams() {
    // TODO: implement getParams
    Map<String,dynamic> map = {
      'title' : this.title,
      'content' : this.content,
      'objectId' : this.objectId,
    };
    return map;
  }
}
